// pages/regimental/regimental.js
var app = getApp(); //引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var page = 1; //页码
var GZHid = 53; //公众号id
var dataLists = {}; //缓存商品列表数据
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nickName: "",
    avatarUrl: "",
    casArray: ['请选择配送小区', '小区1', '小区2', '小区3', '小区4'],
    userName: '',
    xiaoqu: [],
    mobile: '',
    Gender: 'female',
    casIndex: 0,
    xiaoqudw: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function(res) {
        var latitude = res.latitude //维度
        var longitude = res.longitude //经度
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        wx.setStorageSync('latitude', res.latitude),
          wx.setStorageSync('longitude', res.longitude)
        console.log(res, '当前定位！');
        wx.setNavigationBarTitle({
          title: '小区选择'
        })
        wx.request({
          url: commonURL + 'op=shop_area',
          data: {
            uniacid: app.data.GZHid,
            lat: res.latitude,
            lng: res.longitude,
            dev: 1
          },
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            console.log(res);
            var casArray = [];
            var xiaoqu = [];
            console.log(res.data.length == 0)
            if (res.data.length == 0) {
              wx.showModal({
                title: '提示',
                content: '暂时没有团长，去申请团长',
                showCancel: false,
                success: function(res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: '../regimental/regimental',
                    })
                  }
                }
              })
            } else {
              for (var i = 0; i < res.data.length; i++) {
                if (res.data[i]) {
                  casArray.push(res.data[i].name)
                  xiaoqu.push({
                    name: res.data[i].name,
                    id: res.data[i].id,
                    tzname: res.data[i].commander.name,
                    join_hotline: res.data[i].join_hotline,
                    merchant_entry: res.data[i].merchant_entry,
                    avatar: res.data[i].commander.avatar,
                    commanderTel: res.data[i].commander.mobile,
                    distance: res.data[i].distance,
                    service_hotline: res.data[i].service_hotline,
                    address: res.data[i].address
                  })
                }
              }
              console.log(casArray, xiaoqu)
              that.setData({
                casArray: casArray,
                xiaoqu: xiaoqu,
                xiaoqu1: xiaoqu
              })
            }
          }
        })
      },
      fail: function(res) {
        console.log(res)
        if (res.errMsg == 'getLocation:fail auth deny' || res.errMsg == 'getLocation:fail:auth denied') {
          that.setData({
            xiaoqudw: true
          })
        } else if (res.errMsg == 'getLocation:fail 1') {
          that.setData({
            xiaoqudw: true
          })
          wx.showToast({
            icon: 'none',
            title: '请确认GPS已打开，然后重启小程序',
          })
        }
      }
    })
  },
  //搜索
  search: function(e) {
    var value = e.detail.value;
    var casArray = [];
    var xiaoqu = [];
    var that = this;
    var xiaoqu = [];
    for (var i = 0; i < that.data.xiaoqu1.length; i++) {
      if (that.data.xiaoqu1[i].name.indexOf(value) >= 0 || that.data.xiaoqu1[i].tzname.indexOf(value) >= 0) {
        xiaoqu.push(that.data.xiaoqu1[i])
      }
    }
    that.setData({
      xiaoqu: xiaoqu
    })
  },
  getName: function(e) {
    this.setData({
      searchName: e.detail.value
    })
  },
  searchTap: function() {
    var that = this;
    wx.request({ //获取小区地址
      url: commonURL + 'op=shop_area',
      data: {
        uniacid: app.data.GZHid,
        lat: that.data.latitude,
        lng: that.data.longitude,
        dev: 1
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        var casArray = []
        var xiaoqu = []
        if (res.data.length == 0) {
          wx.showModal({
            title: '提示',
            content: '暂时没有团长，去申请团长',
            cancelText: false,
            success: function(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '"../regimental/regimental',
                })
              }
            }
          })
        } else {
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i]) {
              casArray.push(res.data[i].name)
              xiaoqu.push({
                name: res.data[i].name,
                id: res.data[i].id,
                tzname: res.data[i].commander.name,
                join_hotline: res.data[i].join_hotline,
                merchant_entry: res.data[i].merchant_entry,
                service_hotline: res.data[i].service_hotline,
                avatar: res.data[i].commander.avatar
              })
            }
          }
          console.log(casArray, xiaoqu)
          that.setData({
            casArray: casArray,
            xiaoqu: xiaoqu
          })
          wx.setStorageSync('casArray', casArray);
          wx.setStorageSync('xiaoqu', xiaoqu)
        }
      }
    })
  },
  chooseZT: function(e) {
    var that = this
    console.log(e)
    wx.removeStorage({
      key: 'stores',
      success(res) {
        console.log(res.data)
      }
    })
    wx.setStorage({
      key: 'xiaoqu',
      data: e.currentTarget.dataset,
      success: function(res) {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  handler: function(e) {
    if (e.detail.authSetting["scope.userLocation"]) { //如果打开了地理位置，就会为true
      var that = this;
      console.log('11')
      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function(res) {
          console.log(res)
          var latitude = res.latitude
          var longitude = res.longitude
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude
          })
          wx.request({ //获取小区地址
            url: commonURL + 'op=shop_area',
            data: {
              uniacid: app.data.GZHid,
              lat: res.latitude,
              lng: res.longitude,
              dev: 1
            },
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              var casArray = []
              var xiaoqu = []
              console.log(res)
              // console.log(res.data[0])
              for (var i = 0; i < res.data.length; i++) {
                if (res.data[i]) {
                  casArray.push(res.data[i].name)
                  xiaoqu.push({
                    name: res.data[i].name,
                    id: res.data[i].id,
                    tzname: res.data[i].commander.name,
                    join_hotline: res.data[i].join_hotline,
                    service_hotline: res.data[i].service_hotline,
                    merchant_entry: res.data[i].merchant_entry,
                    avatar: res.data[i].commander.avatar
                  })
                }
              }
              console.log(casArray, xiaoqu)
              that.setData({
                casArray: casArray,
                xiaoqu: xiaoqu
              })
              wx.setStorageSync('casArray', casArray);
              wx.setStorageSync('xiaoqu', xiaoqu)
            }
          })
        },
        fail: function(res) {
          wx.showToast({
            title: '授权失败',
          })
        }
      })
    } else {
      console.log('失败')
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    wx.getSetting({
      success: function(res) {
        console.log(res)
        if (res.authSetting['scope.userLocation']) {
          that.setData({
            xiaoqudw: false
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '获取授权信息失败',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})